package com.glureau.aib.client.ai;

import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.MultiUploader;

import java.util.List;

import com.glureau.aib.shared.ai.AiInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AiTable {

	private FlexTable m_mainPanel = new FlexTable();
	private final AiServiceAsync aiService = GWT.create(AiService.class);
	
	public void init() {
		m_mainPanel.setBorderWidth(1);
		aiService.getAiInfos(Cookies.getCookie("sid"), new AsyncCallback<List<AiInfo>>() {
			@Override
			public void onSuccess(List<AiInfo> result) {
				m_mainPanel.setText(0, 0, "Name");
				m_mainPanel.setText(0, 1, "Jar");
				m_mainPanel.setText(0, 2, "Descriptions");
				m_mainPanel.setText(0, 3, "Parameters");
				m_mainPanel.setText(0, 4, "Delete");

				int index = 1;
				for (final AiInfo ai : result) {
					AiChangeHandler aiHandler = new AiChangeHandler();
					aiHandler.setAiInfo(ai);

					TextBox aiName = new TextBox();
					aiName.setName("name");
					aiName.setText(ai.getName());
					aiName.addChangeHandler(aiHandler);
					m_mainPanel.setWidget(index, 1, aiName);

					final SimplePanel sp = new SimplePanel();
					Label uploadLabel = new Label(ai.getAiFileHashcode());
					sp.add(uploadLabel);
					sp.addHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							Object source = event.getSource();
							if (source instanceof Label) {
								MultiUploader uploader = new MultiUploader();
								uploader.setEnabled(true);
								uploader.addOnFinishUploadHandler(onFinishUploaderHandler);
								sp.add(uploader);
							} else {
								Label uploadLabel = new Label(ai.getAiFileHashcode());
								sp.add(uploadLabel);
							}
							sp.remove((Widget) source);
						}
					}, ClickEvent.getType());
					m_mainPanel.setWidget(index, 3, sp);
					index++;
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				m_mainPanel.setText(0, 0, "RPC error : Server unavailable");
				m_mainPanel.setText(1, 0, caught.getMessage());
			}
		});

		
		m_mainPanel.setVisible(true);
	}
	
	// Load the image in the document and in the case of success attach it to
	// the viewer
	private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
		public void onFinish(IUploader uploader) {
			if (uploader.getStatus() == Status.SUCCESS) {
				// The server sends useful information to the client by default
				UploadedInfo info = uploader.getServerInfo();
				String msg = "File name " + info.name + "\n";
				msg += "File content-type " + info.ctype + "\n";
				msg += "File size " + info.size+ "\n";
				msg += "Server message " + info.message;
				Window.alert(msg);
			}
		}
	};

	public Widget getWidget() {
		return m_mainPanel;
	}
}
